package com.matt.sooncleaner.menu

import android.app.Activity
import com.blankj.utilcode.util.SPUtils
import com.google.android.play.core.review.ReviewInfo
import com.google.android.play.core.review.ReviewManagerFactory
import com.google.android.play.core.tasks.Task

/**
 *
 * Created by xiexiang10 on 2024/6/17.
 **/
object RateUtils {
    interface GooglePlayFlowListener {
        fun onCompleteListener()
        fun onErrorListener()
    }

    private const val KEY_CLICK_RATE = "isClickRate"

    fun isClickRate() = SPUtils.getInstance().getBoolean(KEY_CLICK_RATE, false)

    fun setClickRate() {
        SPUtils.getInstance().put(KEY_CLICK_RATE, true)
    }

    fun launchGooglePlay(context: Activity, listener: GooglePlayFlowListener?) {
        val manager = ReviewManagerFactory.create(context)
        val request = manager.requestReviewFlow()
        request.addOnCompleteListener { task: Task<ReviewInfo?> ->
            if (task.isSuccessful) {
                val reviewInfo = task.result
                val flow =
                    manager.launchReviewFlow(context, reviewInfo)
                flow.addOnCompleteListener { task1: Task<Void?>? ->
                    listener?.onCompleteListener()
                }
            } else {
                // There was some problem, log or handle the error code.
                //@ReviewErrorCode int reviewErrorCode = ((TaskException) task.getException()).getErrorCode();
                listener?.onErrorListener()
            }
        }
    }
}